package com.itzdm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itzdm.mapper.AdminMapper;
import com.itzdm.pojo.Admin;
import com.itzdm.pojo.Car;
import com.itzdm.pojo.Result;
import com.itzdm.service.AdminService;
import com.itzdm.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public boolean addVehicle(JSONObject jsonObject) {
        String type = jsonObject.getString("vehicleType");
        String brand = jsonObject.getString("vehicleBrand");
        String model = jsonObject.getString("vehicleModel");
        Double perRent = jsonObject.getDouble("perRent");
        String vehicleLicense = jsonObject.getString("vehicleLicense");
        try {
            adminMapper.addVehicle(new Car(null,type,brand,model,vehicleLicense,perRent,null,null));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public void deleteVehicle(Car car) {
        adminMapper.deleteVehicle(car);
    }

    @Override
    public void updateVehicle(Car car) {
        adminMapper.updateVehicle(car);
    }

    @Override
    public Result login(Admin admin) {
        String username = admin.getUsername();
        int count = adminMapper.isExit(username);
        if (count == 0){
            return Result.error("用户名不存在");
        }
        String pwd = admin.getPwd();
        if (!pwd.equals(adminMapper.getPwd(username))){
            return Result.error("密码输入错误");
        }
        Map<String, Object> redis = new HashMap<>();
        redis.put("username",username);
        redis.put("pwd",pwd);
        String jwt = JwtUtils.generateJwt(redis);
        return Result.success(jwt,"登陆成功");
    }
}
